package com.lib.framework.base

import android.app.Dialog
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.lib.framework.entity.Event
import com.lib.framework.widget.WidgetUtil
import org.greenrobot.eventbus.EventBus


/**
 * Activity 的基类
 *
 * @author keanbin
 */
abstract class BaseActivity : AppCompatActivity(),
    BaseFragment.SendEventToActivityListener {

    companion object {

    }

    protected val mTag = this.javaClass.canonicalName ?: "BaseActivity"

    protected val mContext = this

    /**
     * Loading 对话框
     */
    private var mLoadingDialog: Dialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()

        closeLoadingDialog()
    }

    /**
     * 显示 提示
     */
    fun showPrompt(strResid: Int) {
        showPrompt(getString(strResid))
    }

    /**
     * 显示 提示
     */
    fun showPrompt(str: String?) {
        WidgetUtil.showPromptDialog(this, str ?: "")
    }

    /**
     * 显示 Loading 对话框
     *
     * @param isCancelable  用户是否可以主动取消对话框
     */
    fun showLoadingDialog(isCancelable: Boolean = false, text: String? = null) {
        closeLoadingDialog()

        mLoadingDialog = WidgetUtil.createLoadingDialog(this, text)
        mLoadingDialog?.setCancelable(isCancelable)
        //mLoadingDialog?.setCanceledOnTouchOutside(isCancelable)
        mLoadingDialog?.show()
    }

    /**
     * 关闭 Loading 对话框
     */
    fun closeLoadingDialog() {
        mLoadingDialog?.cancel()
        mLoadingDialog = null
    }

    /**
     * 收到的 Fragment 事件
     */
    override fun onFragmentEvent(event: Event): Boolean {
        when (event.code) {
            BaseFragment.FRAGMENT_EVENT_BACK_BTN_PRESS -> {
                onBackPressed()
                return true
            }
        }

        return false
    }
}